package org.linphone;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Xiang on 2017/11/17.
 */

public class MyAccessibilityService extends AccessibilityService {

	private Map<Integer, Boolean> handleMap = new HashMap<>();
	@Override
	public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
		AccessibilityNodeInfo nodeInfo = accessibilityEvent.getSource();
		if (nodeInfo != null) {
			int eventType = accessibilityEvent.getEventType();
			if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED || eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
				if (handleMap.get(accessibilityEvent.getWindowId()) == null) {
					boolean handled = iterateNodesAndHandle(nodeInfo);
					if (handled) {
						handleMap.put(accessibilityEvent.getWindowId(), true);
					}
				}
			}

		}
	}

	@Override
	public void onInterrupt() {

	}

	//遍历节点，模拟点击安装按钮
	private boolean iterateNodesAndHandle(AccessibilityNodeInfo nodeInfo) {
		if (nodeInfo != null) {
			int childCount = nodeInfo.getChildCount();
			if ("android.widget.Button".equals(nodeInfo.getClassName())) {
				String nodeCotent = nodeInfo.getText().toString();
				if ("安装".equals(nodeCotent) || "完成".equals(nodeCotent) || "确定".equals(nodeCotent)) {
					nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
					return true;
				}
			}
			//遇到ScrollView的时候模拟滑动一下
			else if ("android.widget.ScrollView".equals(nodeInfo.getClassName())) {
				nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
			}
			for (int i = 0; i < childCount; i++) {
				AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
				if (iterateNodesAndHandle(childNodeInfo)) {
					return true;
				}
			}
		}
		return false;
	}
}
